package com.beta.cat.utils;

/**
 * 公司内部常用的用户id之间的转换
 *
 * @author taofucheng
 * @date 2022/04/29
 */
public class UserIdConvertUtils {
    /**
     * 位移量，BetaUnionId其高位表示渠道码，低32位存放对应系统的账号，所以与业务中id转换时，要将渠道码的后32位空间让出来。
     */
    private static final int shiftVal = 32;//注意：转换成10进制是 4294967296

    /**
     * 1. betaUnionId 转 internalUserId
     *
     * @param betaUnionId betaUnionId
     * @return java.lang.Integer internalUserId
     */
    public static Integer betaUnionIdToInternalUserId(ChannelIdEnum channelId, Long betaUnionId) {
        Long internalUserId = betaUnionId - (channelId.getId() << shiftVal);
        return internalUserId.intValue();
    }

    /**
     * 1. internalUserId 转 betaUnionId
     *
     * @param internalUserId internalUserId
     * @return java.lang.Integer betaUnionId
     */
    public static Long internalUserIdToBetaUnionId(ChannelIdEnum channelId, Integer internalUserId) {
        return (channelId.getId() << shiftVal) + internalUserId;
    }

    /**
     * 渠道码
     */
    public static enum ChannelIdEnum {

        BETA_CHANNEL_ID("9ZLDS", "Beta理财师、查私募、上善的渠道Id，用于internalUserId转换"),
        BETA_2C_CHANNEL_ID("9ZLDT", "toC的渠道"),
        INTERNET_ID_CHANNEL_ID("JZ6RL", "InternetId的转换规则"),
        ;

        private static final String NUM_DIC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        private static final int NUM_BASE = 36;

        ChannelIdEnum(String type, String descript) {
            this.type = type;
            this.descript = descript;
        }

        private String type;
        private String descript;

        public Long getId() {
            return Long.valueOf(type, NUM_BASE);
        }

        public String getType() {
            return type;
        }

        public String getDescript() {
            return descript;
        }

    }
}
